﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.UI.Views
{
    /// <summary>
    /// HintView.xaml 的交互逻辑
    /// </summary>
    public partial class HintView : Window
    {
        public HintView()
        {
            InitializeComponent();
        }

        public HintView(int duringShowTime, int stayTime, double width, double height)
        {
            InitializeComponent();
            this.Width = width + 10;
            this.Height = height + 10;
            _border.Width = width;
            _border.Height = height;
            Canvas.SetLeft(this._border, -10 - width);
            this._duringShowTime = duringShowTime;
            this._stayTime = stayTime;
        }

        private Storyboard _storyboard;
        private DoubleAnimation _animationShow;
        private DoubleAnimation _animationHide;
        private int _duringShowTime = 300;
        private int _stayTime = 3000;

        public void SetText(string txt, InfoType it = InfoType.Info)
        {
            tb.Text = txt;
            if (it == InfoType.Info)
            {
                _border.Background = new SolidColorBrush(Color.FromArgb(0xFF, 0x00, 0xFF, 0x00));
            }
            else if (it == InfoType.Warning)
            {
                _border.Background = new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0xFF, 0x00));
            }
            else
            {
                _border.Background = new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0x00, 0x00));
            }
            //
            int duringTime = _duringShowTime;
            //计算出现动画的时间
            duringTime = Math.Abs((int)(Canvas.GetLeft(_border) * _duringShowTime / _border.Width));
            if (_storyboard != null)
            {
                _animationShow.Duration = new TimeSpan(0, 0, 0, 0, duringTime);
                _animationShow.From = Canvas.GetLeft(_border);
                _animationShow.To = 5;
            }
            else
            {
                _storyboard = new Storyboard();
                _storyboard.Completed += (sender, e) =>
                {
                    if (_storyboard == null)
                    {
                        _border.BeginAnimation(Canvas.LeftProperty, null);
                        Canvas.SetLeft(_border, -10 - _border.Width);
                    }
                };
                System.Diagnostics.Debug.WriteLine("duringTime:{0},Left:{1}", duringTime, this.Left);
                _storyboard.Completed += sb_Completed;
                //出来的动画
                _animationShow = new DoubleAnimation();
                _animationShow.From = Canvas.GetLeft(_border);
                _animationShow.To = 5;
                _animationShow.Duration = new TimeSpan(0, 0, 0, 0, duringTime);
                Storyboard.SetTarget(_animationShow, _border);
                Storyboard.SetTargetProperty(_animationShow, new PropertyPath("(Canvas.Left)"));
                _storyboard.Children.Add(_animationShow);
                //回去的动画
                _animationHide = new DoubleAnimation();
                _animationHide.From = 5;
                _animationHide.To = -10 - _border.Width;
                _animationHide.Duration = new TimeSpan(0, 0, 0, 0, _duringShowTime);
                _animationHide.BeginTime = new TimeSpan(0, 0, 0, 0, _stayTime + duringTime);
                Storyboard.SetTarget(_animationHide, _border);
                Storyboard.SetTargetProperty(_animationHide, new PropertyPath("(Canvas.Left)"));
                _storyboard.Children.Add(_animationHide);
            }
            _storyboard.Begin();
        }

        void sb_Completed(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// 信息类型
        /// </summary>
        public enum InfoType
        {
            Info,
            Warning,
            Error,
        }
    }
}
